package com.shark.security.jwt.domain;

import java.io.Serializable;

import com.shark.security.jwt.constant.MessageCode;

import lombok.Data;

@Data
public final class JsonResponse<T> implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 5144122008684523408L;

	private String code;

    private String msg;

    private T data;

    public static <T> JsonResponse<T> success(T d) {
        JsonResponse<T> resp = new JsonResponse<>();
        resp.setCode(MessageCode.Success.getCode());
        resp.setMsg(MessageCode.Success.getMessage());
        resp.setData(d);
        return resp;
    }

    public static <T> JsonResponse<T> success(String msg, T d) {
        JsonResponse<T> resp = new JsonResponse<>();
        resp.setCode(MessageCode.Success.getCode());
        resp.setMsg(msg);
        resp.setData(d);
        return resp;
    }

    public static <T> JsonResponse<T> error(String msg) {
        JsonResponse<T> resp = new JsonResponse<>();
        resp.setCode(MessageCode.Failed.getCode());
        resp.setMsg(msg);
        return resp;
    }

    public static <T> JsonResponse<T> error(MessageCode msg) {
        JsonResponse<T> resp = new JsonResponse<>();
        resp.setCode(msg.getCode());
        resp.setMsg(msg.getMessage());
        return resp;
    }

    public static <T> JsonResponse<T> error(MessageCode msg, T d) {
        JsonResponse<T> resp = new JsonResponse<>();
        resp.setCode(msg.getCode());
        resp.setMsg(msg.getMessage());
        resp.setData(d);
        return resp;
    }

    public static <T> JsonResponse<T> error(Exception e) {
        JsonResponse<T> resp = new JsonResponse<>();
        resp.setCode(MessageCode.Failed.getCode());
        resp.setMsg(e.getMessage());
        return resp;
    }
}
